package com.whfc.ms.api.sys.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.dto.SysRoleDTO;
import com.whfc.fuum.dto.SysRoleDetailDTO;
import com.whfc.fuum.dto.SysRuleDTO;
import com.whfc.fuum.dto.SysUserDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.SysRoleAdd;
import com.whfc.fuum.param.SysRoleEdit;
import com.whfc.fuum.param.SysRoleRuleEdit;
import com.whfc.ms.api.sys.service.MsSysRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequiresPermissions(value = {"sys/role", "sys-gscx/role"}, logical = Logical.OR)
@RestController
@RequestMapping("/ms/api/role")
public class SysRoleController extends BaseController {

    @Autowired
    private MsSysRoleService sysRoleService;


    @GetMapping(value = "/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                       @RequestParam(value = "deptId", required = false) Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("获取角色列表，loginUser:{},deptId:{}, keyword:{}", loginUser.getUsername(), deptId, keyword);
        List<SysRoleDTO> list = sysRoleService.list(loginUser.getId(), deptId, keyword);
        return ResultUtil.success(new ListData<>(list));
    }

    /**
     * 获得角色详情
     *
     * @param loginUser
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                         @RequestParam("id") Integer id,
                         @RequestParam("platform") Integer platform) {
        logger.info("获得角色详情，loginUser:{},id:{},platform:{}", loginUser.getUsername(), id, platform);
        SysRoleDetailDTO sysRoleDetailDTO = sysRoleService.getRoleDetailById(id, platform);
        if (sysRoleDetailDTO == null) {
            return ResultUtil.paramError("未找到数据");
        }
        return ResultUtil.success(sysRoleDetailDTO);
    }

    /**
     * 添加角色
     *
     * @param loginUser
     * @param request
     * @return
     */
    @RequiresPermissions(value = {"sys/role/add", "sys-gscx/role/add"}, logical = Logical.OR)
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                      @Validated @RequestBody SysRoleAdd request) {
        logger.info("添加角色,loginUser:{},data:{}", loginUser.getUsername(), request);
        request.setCreateBy(loginUser.getId());
        sysRoleService.add(request);
        return ResultUtil.success();
    }


    /**
     * 删除角色
     *
     * @param loginUser
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"sys/role/del", "sys-gscx/role/del"}, logical = Logical.OR)
    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                      @PathVariable("id") Integer id) {
        logger.info("删除角色，loginUser:{},id:{}", loginUser.getUsername(), id);
        sysRoleService.del(id);
        return ResultUtil.success();
    }


    /**
     * 编辑角色
     *
     * @param loginUser
     * @param request
     * @return
     */
    @RequiresPermissions(value = {"sys/role/edit", "sys-gscx/role/edit"}, logical = Logical.OR)
    @PostMapping("edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                       @Validated @RequestBody SysRoleEdit request) {
        logger.info("编辑角色，loginUser:{},data:{}", loginUser.getUsername(), request);
        sysRoleService.edit(request);
        return ResultUtil.success();
    }


    @GetMapping("/user/list")
    public Result getRoleUserList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("roleId") Integer roleId) {
        logger.info("查询角色关联的用户列表,loginUser:{},roleId:{}", user.getUsername(), roleId);
        List<SysUserDTO> list = sysRoleService.getRoleUserList(roleId);
        return ResultUtil.success(new ListData<>(list));
    }

    @GetMapping("/rule/list")
    public Result getRoleRuleList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("roleId") Integer roleId,
                                  @RequestParam("platform") Integer platform) {
        logger.info("查询角色关联的权限列表,loginUser:{},roleId:{},platform:{}", user.getUsername(), roleId, platform);
        List<SysRuleDTO> list = sysRoleService.getRoleRuleList(roleId, platform);
        return ResultUtil.success(new ListData<>(list));
    }

    @RequiresPermissions(value = {"sys/role/edit", "sys-gscx/role/edit"}, logical = Logical.OR)
    @PostMapping("/rule/edit")
    public Result editRoleRule(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @Validated @RequestBody SysRoleRuleEdit params) {
        logger.info("编辑角色关联的权限列表,loginUser:{},params:{}", user.getUsername(), params);
        sysRoleService.editRoleRule(params);
        return ResultUtil.success();
    }
}
